/**
 *	Copyright (c) 2009, SpLib.net
 * 	All rights reserved.
  	
 * 	Author:6spring
 * 	About:多类多帧图片动画
*/

package SpLib.display
{
	import SpLib.ObjSys.BaseObj;
	import SpLib.std.iterator;
	import SpLib.std.map;
	
	public class MutiAni extends SpLib.display.Wnd
	{
		private var m_sDefPakName:String = "";
		private var m_szAni:SpLib.std.map = new SpLib.std.map();
		private var m_pCurAni:SpLib.display.Ani = null;
		
		public function MutiAni(father:BaseObj = null,pakname:String = "")
		{
			super(father);
			m_sDefPakName = pakname;
		}
		
		public function Clear():void
		{
			if(m_pCurAni!=null)
			{
				m_pCurAni.StopAni();
				m_pCurAni = null;
			}
			
			var iter:SpLib.std.iterator = m_szAni.begin();
			while(iter!=m_szAni.end())
			{
				var ani:SpLib.display.Ani = iter.m_Data as SpLib.display.Ani;
				ani.DetachImg();
				iter = iter.next;
			}
			
			m_sDefPakName = "";
			
			m_szAni.clear();
		}
		
		public function AddAni(name:String,file:String,pakname:String =""):Boolean
		{
			if(pakname.length==0)
			{
				pakname = m_sDefPakName;
			}
			
			var ani:SpLib.display.Ani = new SpLib.display.Ani();
			if(ani.SetSrc(pakname,file))
			{
				var w:int = Math.max(m_Rect.width,ani.width);
				var h:int = Math.max(m_Rect.height,ani.height);
				if(w!=width || h!=height)
				{
					width = w;
					height = h;
				}
				
				m_szAni.setvalue(name,ani);
				return true;
			}
			
			return false;
		}
		
		public function SetAni(name:String,delay:int = -1):void
		{
			var iter:SpLib.std.iterator = m_szAni.find(name);
			if(iter!=m_szAni.end())
			{
				if(m_pCurAni!=null)
				{
					if(m_pCurAni==iter.m_Data)
					{
						return;
					}
					
					m_pCurAni.StopAni();
				}
				
				m_pCurAni = iter.m_Data as SpLib.display.Ani;
			}
			
			if(delay!=-1)
			{
				PlayAni(delay);
			}
		}
		
		public function PlayAni(delay:int = 200):void
		{
			if(m_pCurAni!=null)
			{
				m_pCurAni.PlayAni(0,-1,delay);
			}
		}
		
		public function StopAni():void
		{
			if(m_pCurAni!=null)
			{
				m_pCurAni.StopAni();
			}
		}
		
		public function get playing():Boolean
		{	
			return m_pCurAni!=null?m_pCurAni.playing:false;
		}
		
		override protected function OnDrawSelf(dc:SpLib.display.Graphics2D):Boolean
		{
			if(m_pCurAni!=null)
			{
				m_pCurAni.alpha = super.alpha;
				m_pCurAni.OnDraw(dc);
			}
			
			return false;
		}
	}
}